//
//  StartDataCell.swift
//  socksTest
//
//  Created by liujianlong on 2023/6/21.
//

import UIKit
import CoreBluetooth

class StartDataCell: UITableViewCell {
    @IBOutlet var name: UILabel!
    @IBOutlet var battery: UILabel!
    @IBOutlet var temp: UILabel!
    @IBOutlet var low: UIButton!
    @IBOutlet var mid: UIButton!
    @IBOutlet var high: UIButton!
    @IBOutlet var slider: UISlider!
    @IBOutlet var min_num: UILabel!
    @IBOutlet var max_num: UILabel!
    @IBOutlet var select_time: UIButton!
    @IBOutlet var switchBtn: UISwitch!
    @IBOutlet var currentValue: UILabel!
    var peripheral : CBPeripheral?
    let popView : MMPopViewController = MMPopViewController()
    var block:((_ title:String, _ vale:Any)->Void)?
    override func awakeFromNib() {
        super.awakeFromNib()
//        slider.isContinuous = false
        popView.array = ["关闭定时","1","10","30","60","90","120"]
        popView.delegate = self
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
    
    @IBAction func actionForSwitch(_ sender: UISwitch) {
        block?("开关机",sender.isOn)
    }
    
    @IBAction func actionForSelectTime(_ sender: UIButton) {
        popView.modalPresentationStyle = .popover
        popView.preferredContentSize = CGSize(width:150, height:popView.array.count * 50)
        popView.popover = popView.popoverPresentationController
        popView.popover?.delegate = self
        popView.popover!.permittedArrowDirections = .up
        popView.popoverPresentationController?.sourceView = sender
        popView.popoverPresentationController?.sourceRect = sender.bounds
        self.currentVC().present(popView, animated:true, completion:nil)
    }
    
    @IBAction func actionForBtn(_ sender: UIButton) {
        updateBtn(sender)
        block?("设置温度_按钮",sender.tag)
    }
    
    @IBAction func actionForSliderValueChange(_ sender: UISlider) {
//        block?("设置温度_滑动",Int(round(slider.value)))
    }
    
    @IBAction func actionForWillchange(_ sender: UISlider) {
        let value = Int(round(slider.value))
        sender.setValue(Float(value), animated: false)
        currentValue.text = "\(value)"
        currentValue.isHidden = false
        block?("设置温度_滑动",Int(round(slider.value)))
    }
    func updateBtn(_ sender:UIButton){
        low.bgHexColor = low == sender ? "#02CDDA" : "#EEEEEE"
        mid.bgHexColor = mid == sender ? "#02CDDA" : "#EEEEEE"
        high.bgHexColor = high == sender ? "#02CDDA" : "#EEEEEE"
        low.isSelected = low == sender
        mid.isSelected = mid == sender
        high.isSelected = high == sender
    }
    
    func config() {
        guard let per = peripheral else{return}
        name.text = per.model.name
        switchBtn.isOn = per.model.ison
        battery.text = "电量: " + (per.model.ison ? "\(per.model.elect)" : "-") + "%"
        temp.text = "当前温度: " + (per.model.ison ? "\(per.model.current_t)" : "-") + "°C"
        select_time.setTitle("\(per.model.off_time)", for: .normal)
        
        slider.isEnabled = per.model.ison
        low.isEnabled = per.model.ison
        mid.isEnabled = per.model.ison
        high.isEnabled = per.model.ison
        select_time.isEnabled = per.model.ison
        
        switch per.model.change{
        case .normal:
            slider.value = slider.minimumValue
            currentValue.isHidden = true
            updateBtn(UIButton())
        case .btn:
            slider.value = slider.minimumValue
            currentValue.isHidden = true
            if per.model.targetValue == 10000{return}
            let t = per.model.targetValue != per.model.config_t ? per.model.targetValue : per.model.config_t
            if t >= 35 && t <= 38{
                updateBtn(low)
            }else if t >= 39 && t <= 42{
                updateBtn(mid)
            }else if t >= 43 && t <= 45{
                updateBtn(high)
            }
        case .slider:
            updateBtn(UIButton())
            if per.model.targetValue == 10000{return}
            let t = per.model.targetValue != per.model.config_t ? per.model.targetValue : per.model.config_t
            slider.value = Float(t)
            currentValue.text = "\(Int(t))"
            currentValue.isHidden = false
        }
    }
}


//MARK: - MMPopViewControllerDelegate (UIPopoverPresentationControllerDelegate)
extension StartDataCell : MMPopViewControllerDelegate{
    func select(_ index: Int, _ title: String) {
        let min = title == popView.array.first ? 0 : (Int(title) ?? 0)
        select_time.setTitle("\(min)", for: .normal)
        block?("选择时长",min)
    }
    
    /// 默认返回的是覆盖整个屏幕，需设置成UIModalPresentationNone。
    func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
        return .none
    }
    
    func popoverPresentationControllerShouldDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) -> Bool {
        return true
    }
}
